One program can call another program as a subroutine. The subroutine can be external (a separate program) or internal (included in the main program). Subroutines are useful when a program needs to repeat the same group of commands at several different places.
To call a separate program, use the same syntax that you use to run the program from the entry line.
To define an internal subroutine, use the Define command with Prgm...EndPrgm. Because a subroutine must be defined before it can be called, it is a good practice to define subroutines at the beginning of the main program.
An internal subroutine is called and executed in the same way as a separate program.
Define subtest1()= Prgm local subtest2 À Define subtest2(x,y)= Á Prgm Disp x,y EndPrgm ©Beginning of main program For i,1,4,1 subtest2(i,I*1000) Â EndFor EndPrgm |
À |
Declares the subroutine as a local variable. |
Á |
Defines the subroutine. |
 |
Calls the subroutine. |
Note: Use the Program Editor’s Var menu to enter the Define and Prgm...EndPrgm commands.
At the end of a subroutine, execution returns to the calling program. To exit a subroutine at any other time, use Return with no argument.
A subroutine cannot access local variables declared in the calling program. Likewise, the calling program cannot access local variables declared in a subroutine.
Lbl commands are local to the programs in which they are located. Therefore, a Goto command in the calling program cannot branch to a label in a subroutine or vice versa.
When evaluating a user-defined function or running a program, you can specify an argument that includes the same variable that was used to define the function or create the program. However, to avoid circular-definition errors, you must assign a value for variables that are used in evaluating the function or running the program. For example:
x+1&x À |
– or –
For i,i,10,1 Disp i À EndFor |
À |
Causes a Circular definition error message if x or i does not have a value. The error does not occur if x or i has already been assigned a value. |